using System.Collections.Generic;
using UnityEngine;

public class BillboardArea : MonoBehaviour
{
	private int _count;

	public int Count;

	public float AdditionalHeight;

	public int Seed;

	public GameObject PrefabObj;

	public BillboardDummy[] billboardDummies;

	private Mesh _mesh;

	private List<TriangleForBillboardArea> TriangleList;

	private Camera _camera;

	private float maxRange;

	private int frame;

	private void Start()
	{
		base.enabled = false;
		_camera = Camera.main;
		frame = Random.Range(0, 3);
	}

	private void Init()
	{
		TriangleList = new List<TriangleForBillboardArea>();
		_mesh = GetComponent<MeshFilter>().sharedMesh;
		float num = 0f;
		int num2 = _mesh.triangles.Length / 3;
		maxRange = 0f;
		for (int i = 0; i < _mesh.vertices.Length; i++)
		{
			float sqrMagnitude = _mesh.vertices[i].sqrMagnitude;
			if (maxRange < sqrMagnitude)
			{
				maxRange = sqrMagnitude;
			}
		}
		for (int j = 0; j < num2; j++)
		{
			TriangleForBillboardArea triangleForBillboardArea = new TriangleForBillboardArea();
			triangleForBillboardArea.Vertices = new Vector3[3];
			triangleForBillboardArea.Vertices[0] = _mesh.vertices[_mesh.triangles[3 * j]];
			triangleForBillboardArea.Vertices[1] = _mesh.vertices[_mesh.triangles[3 * j + 1]];
			triangleForBillboardArea.Vertices[2] = _mesh.vertices[_mesh.triangles[3 * j + 2]];
			triangleForBillboardArea.Square = Vector3.Dot(triangleForBillboardArea.Vertices[1] - triangleForBillboardArea.Vertices[0], triangleForBillboardArea.Vertices[2] - triangleForBillboardArea.Vertices[0]);
			num += triangleForBillboardArea.Square;
			TriangleList.Add(triangleForBillboardArea);
		}
		if (TriangleList.Count == 0 || num == 0f)
		{
			TriangleList.Clear();
			return;
		}
		float num3 = 0f;
		for (int k = 0; k < num2; k++)
		{
			if (k < num2 - 1)
			{
				TriangleList[k].Weight = TriangleList[k].Square / num;
			}
			else
			{
				TriangleList[k].Weight = 1f - num3;
			}
			num3 += TriangleList[k].Weight;
		}
	}

	private Vector3 GetRandomPoint()
	{
		TriangleForBillboardArea randomTriangle = GetRandomTriangle();
		return GetRandomPointInTriangle(randomTriangle.Vertices[0], randomTriangle.Vertices[1], randomTriangle.Vertices[2]);
	}

	private TriangleForBillboardArea GetRandomTriangle()
	{
		if (TriangleList == null || TriangleList.Count == 0)
		{
			Init();
		}
		if (TriangleList == null || TriangleList.Count == 0)
		{
			return null;
		}
		float num = Random.value;
		for (int i = 0; i < TriangleList.Count; i++)
		{
			if (num <= TriangleList[i].Weight)
			{
				return TriangleList[i];
			}
			num -= TriangleList[i].Weight;
		}
		return null;
	}

	private Vector3 GetRandomPointInTriangle(Vector3 a, Vector3 b, Vector3 c)
	{
		Vector3 vector = b - a;
		Vector3 vector2 = c - a;
		float num = Random.value;
		float num2 = Random.value;
		if (num + num2 >= 1f)
		{
			num = 1f - num;
			num2 = 1f - num2;
		}
		return a + num * vector + num2 * vector2;
	}

	private void Generate()
	{
		Random.seed = Seed;
		ClearAll();
		billboardDummies = new BillboardDummy[Count];
		for (int i = 0; i < billboardDummies.Length; i++)
		{
			billboardDummies[i] = new BillboardDummy();
			billboardDummies[i].Position = base.transform.position + GetRandomPoint();
			billboardDummies[i].Parent = base.transform;
		}
	}

	private void ClearAll()
	{
		if (billboardDummies == null || billboardDummies.Length <= 0)
		{
			return;
		}
		for (int i = 0; i < billboardDummies.Length; i++)
		{
			if (billboardDummies[i] != null)
			{
				billboardDummies[i].Hide();
			}
		}
	}

	private void CheckAllBillboards()
	{
		if (billboardDummies == null || billboardDummies.Length == 0)
		{
			return;
		}
		if (Application.isPlaying)
		{
			if (Time.frameCount % 3 != frame || (base.transform.position - _camera.transform.position).sqrMagnitude > maxRange * 2f + 13000f)
			{
				return;
			}
			for (int i = 0; i < billboardDummies.Length; i++)
			{
				if (billboardDummies[i].BillboardUnit == null && (billboardDummies[i].Position - _camera.transform.position).sqrMagnitude <= 10000f)
				{
					billboardDummies[i].Show(PrefabObj);
				}
				else if (billboardDummies[i].BillboardUnit != null && (billboardDummies[i].Position - _camera.transform.position).sqrMagnitude > 12000f)
				{
					billboardDummies[i].Hide();
				}
			}
			return;
		}
		for (int j = 0; j < billboardDummies.Length; j++)
		{
			if (billboardDummies[j].BillboardUnit == null)
			{
				billboardDummies[j].Show(PrefabObj);
			}
		}
	}

	private void Update()
	{
		if (Device.isWeakDevice)
		{
			base.enabled = false;
			return;
		}
		if (Count != _count)
		{
			_count = Count;
			Generate();
		}
		CheckAllBillboards();
	}
}
